program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement=^element;
  element=record
    a: integer;
    k:Integer;
    next: pelement;
  end;
procedure AddStartN(var headn: pelement; a, k: Integer);
var
  tmp: pelement;
begin
  tmp:=headn;
  New(headn);
  headn^.next:=tmp;
  headn^.a:=a;
  headn^.k:=k;
end;
procedure AddStartM(var headm: pelement; a, k: Integer);
var
  tmp: pelement;
begin
  tmp:=headm;
  New(headm);
  headm^.next:=tmp;
  headm^.a:=a;
  headm^.k:=k;
end;
var
  n, m, i, a, k: integer;
  fin: TextFile;
  headn, headm: pelement;
  curn, curm: pelement;
  g, h, j, l, s, d: integer;
begin
  AssignFile(fin, 'input.txt');
  Reset(fin);
  headn:=nil;
  headm:=nil;
  Readln(fin, n);
  for i:=0 to n-1 do
  begin
    readln(fin, a, k);
    AddStartN(headn, a, k);
  end;
  Readln(fin, m);
  for i:=0 to m-1 do
  begin
    readln(fin, a, k);
    AddStartM(headm, a, k);
  end;
  curn:=headn;
  curm:=headm;
  if (curn^.k>curm^.k) then
  begin
    g:=curn^.a;
    h:=curn^.k;
    write('(', g, ') * ', 'x^ ', h);
    curn:=curn^.next;
  end;
  if (curn^.k<curm^.k) then
  begin
    j:=curm^.a;
    l:=curm^.k;
    write('(', j, ') * ', 'x^ ', l);
    curm:=curm^.next;
  end;
  if (curn^.k=curm^.k) then
  begin
    s:=curn^.a+curm^.a;
    d:=curn^.k;
    write('(', s, ') * ', 'x^ ', d);
    curm:=curm^.next;
    curn:=curn^.next;
  end;
  while(curn<>nil) and (curm<>nil) do
  begin
    if (curn^.k>curm^.k) then
    begin
      g:=curn^.a;
      h:=curn^.k;
      write('+ (', g, ') * ', 'x^ ', h);
      curn:=curn^.next;
    end;
    if (curn^.k<curm^.k) then
    begin
      j:=curm^.a;
      l:=curm^.k;
      write('+ (', j, ') * ', 'x^ ', l);
      curm:=curm^.next;
    end;
    if (curn^.k=curm^.k) then
    begin
      s:=curn^.a+curm^.a;
      d:=curn^.k;
      write('+ (', s, ') * ', 'x^ ', d);
      curm:=curm^.next;
      curn:=curn^.next;
    end;
  end;
  write(' =0');
  Readln;
end.
 